<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Upload as UploadModel;
use think\Cache;
use think\Db;
use org\Http;

/**
 * 系统配置
 * Class System
 * @package app\admin\controller
 */
class System extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 站点配置
     */
    public function siteConfig()
    {
        $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
        $site_config = unserialize($site_config['value']);
        return $this->fetch('site_config', ['site_config' => $site_config]);
    }

    /**
     * 更新配置
     */
    public function updateSiteConfig()
    {
        if ($this->request->isPost()) {
            $site_config = $this->request->post('site_config/a');
            $site_config['site_tongji'] = htmlspecialchars_decode($site_config['site_tongji']);
            $data['value'] = serialize($site_config);

            if (Db::name('system')->where('name', 'site_config')->update($data) !== false) {
                Cache::set('site_config', null);
                return json(array('code' => 200, 'msg' => '提交成功'));
            } else {
                return json(array('code' => 200, 'msg' => '提交失败'));
            }
        }
    }

    public function get_theme_info($tpl_name)
    {
        $theme_url = DS . 'template' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $theme_url . $tpl_name . DS)) {
            $info = include ROOT_PATH . $theme_url . $tpl_name . DS . 'config.php';
            $info['image'] = WEB_URL . $theme_url . $tpl_name . DS . 'images' . DS . 'covershow.png';
            $info['tpl_name'] = $tpl_name;
            $info['tpl_name'] = $tpl_name;
            $info['tpl_name'] = $tpl_name;
            return $info;
        }
    }

    public function template()
    {
        //数据库获取
        $tpl_use = 'layres';
        $res = Db::name('system')->where('name', 'template')->find();
        if ($res) {
            $tpl_use = $res['value'];
        } else {
            $data['name'] = 'template';
            $data['value'] = $tpl_use;
            Db::name('system')->insert($data);
        }
        $theme_list = array();
        $usedTpl = $this->get_theme_info($tpl_use);

        $theme_array = get_subdirs(ROOT_PATH . 'template' . DS);
        foreach ($theme_array as $tpl_name) {

            if ($tpl_name == $tpl_use) {
                continue;
            }

            $theme_list[] = $this->get_theme_info($tpl_name);

        }

        $this->assign('used_tpl', $usedTpl);

        $this->assign('theme_list', $theme_list);
        return view();
    }

    public function usetpl($tpl_name)
    {
        $theme_array = get_subdirs(ROOT_PATH . 'template' . DS);
        if (in_array($tpl_name, $theme_array)) { // 判断删除操作的模板是否真实存在
            // 替换系统设置中模板值

            $res = Db::name('system')->where('name', 'template')->update(['value' => $tpl_name]);
            if ($res) {
                $config = array(
                    'TPL_DIR' => $tpl_name,
                );
                $path = 'application/extra/web.php';
                $str = '<?php return [';
                foreach ($config as $key => $value) {
                    $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
                };
                $str .= ']; ';
                file_put_contents($path, $str);

                return json(array('code' => 200, 'msg' => '模板切换成功'));

            } else {
                return json(array('code' => 0, 'msg' => '没做任何修改'));
            }
        }

    }


    /**
     * 清除缓存
     */
    public function clear()
    {
        delete_dir_file(CACHE_PATH);
        array_map('unlink', glob(TEMP_PATH . '/*.php'));
        if (!file_exists(TEMP_PATH)) {
            return json(array('code' => 200, 'msg' => '暂无缓存'));
        } else {
            rmdir(TEMP_PATH);
            return json(array('code' => 200, 'msg' => '更新缓存成功'));
        }

    }

    public function douploadpic()
    {
        if (!session('admin_id')) {
            return "error|没有登录";
        } else {
            $uploadmodel = new UploadModel();
            $info = $uploadmodel->upfile("images", "FileName");
            return $info['data']['src'];

        }
    }

}
